package id.net.iconpln.bb.client.view;

import id.net.iconpln.bb.client.data.TestData;
import id.net.iconpln.bb.client.model.PageBbPemasokBean;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.user.client.Element;

public class PageBbVerifikasihasilujisurveyor extends LayoutContainer {
	
	Grid<PageBbPemasokBean> grid;
	private FormData formData;
	private FormBinding formBindings;
	
	@Override
	  protected void onRender(Element parent, int pos) {
	    super.onRender(parent, pos); 
	    setLayout(new FitLayout()); 
	    
	    ContentPanel cp = new ContentPanel();  
	  
	    cp.setHeading("Daftar Hasil Sampling dari Surveyor");  
	    cp.setFrame(true);  
	    cp.setSize(1030, 600);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));  
	  
	    grid = createGrid();
	    grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
	    grid.getSelectionModel().addListener(Events.SelectionChange,  
	            new Listener<SelectionChangedEvent<PageBbPemasokBean>>() {  
	              public void handleEvent(SelectionChangedEvent<PageBbPemasokBean> be) {  
	                if (be.getSelection().size() > 0) {  
	                  formBindings.bind((ModelData) be.getSelection().get(0));  
	                } else {  
	                  formBindings.unbind();  
	                }  
	              }  
	            });  
	    FormPanel panel = createForm();
	    formBindings = new FormBinding(panel, true); 
	  
	    cp.add(panel, new RowData(.4, 1));  
	    cp.add(grid, new RowData(.7, 1)); 
	    add(cp, new FitData(5)); 
	  }  
	  
	  private FormPanel createForm() {  
	    FormPanel panel = new FormPanel();  
	    panel.setHeaderVisible(false);
	    
	    FieldSet fieldSet2 = new FieldSet();  
	    fieldSet2.setHeading("Verifikasi Hasil Uji");  
	    fieldSet2.setCollapsible(false);

	    FormLayout layout2 = new FormLayout();  
	    layout2.setLabelWidth(150);  
	    fieldSet2.setLayout(layout2); 
	    
	    final SimpleComboBox<String> txtconfirm = new SimpleComboBox<String>();  
	    txtconfirm.setFieldLabel("Persetujuan");  
	    txtconfirm.setForceSelection(true);  
	    txtconfirm.setTypeAhead(true);   
	    txtconfirm.setTriggerAction(TriggerAction.ALL);
	    txtconfirm.add("Confirm");
	    txtconfirm.add("Pending");
	    txtconfirm.add("Reject");
	    fieldSet2.add(txtconfirm, formData);
	    
	    final TextField<String> txtharga = new TextField<String>();
	    txtharga.setName("harga");
	    txtharga.setFieldLabel("Harga");  
	    fieldSet2.add(txtharga, formData);
	    
	    final TextField<String> txtba = new TextField<String>();
	    txtba.setName("ba");
	    txtba.setFieldLabel("BA Terima");  
	    fieldSet2.add(txtba, formData);  
	    
	    panel.add(fieldSet2);
	    
	    ContentPanel p = new ContentPanel();
	    p.setHeaderVisible(false);	    
	    p.setButtonAlign(HorizontalAlignment.RIGHT);  
	    p.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().rejectChanges();
	      }  
	    }));  
	    p.addButton(new Button("Submit", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().commitChanges();
	      }  
	    }));  

	    panel.add(p);
	    
	   return panel;  
	  }  
	  
	  private Grid<PageBbPemasokBean> createGrid() {  
	    
	   
	    final List<ColumnConfig> columns = new ArrayList<ColumnConfig>(); 
	    
	    RowNumberer id = new RowNumberer();  
	    id.setWidth(30);
	    
	    ColumnConfig noagenda = new ColumnConfig("", "No Agenda", 200);
	    noagenda.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig jml = new ColumnConfig("", "Jumlah Sampling", 150);
	    jml.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig tgl = new ColumnConfig("", "Tanggal Pengambilan", 150);
	    tgl.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig kualitas = new ColumnConfig("", "Kualitas", 150);
	    kualitas.setAlignment(HorizontalAlignment.LEFT);
	    
	    columns.add(id);
	    columns.add(noagenda);
	    columns.add(jml);
	    columns.add(tgl);
	    columns.add(kualitas);
	   
	    ListStore<PageBbPemasokBean> store = new ListStore<PageBbPemasokBean>();  
	    store.setMonitorChanges(true);  
//	    store.add(TestData.getPemasok());  
	  
	    final ColumnModel cm = new ColumnModel(columns);
	    
	    grid = new Grid<PageBbPemasokBean>(store, cm);
	    grid.setBorders(true);  
	    
	    return grid;  
	  }  
}
